HackerRank Queen's Attack II
提出
code: python
import math
import os
import random
import re
import sys
#
# Complete the 'queensAttack' function below.
#
# The function is expected to return an INTEGER.
# The function accepts following parameters:
# 1. INTEGER n area size
# 2. INTEGER k number of obstacle
# 3. INTEGER r_q place
# 4. INTEGER c_q place
# 5. 2D_INTEGER_ARRAY obstacles
#
# def horizontal(r_q, c_q, obstacles):
# left_res = float('inf')
# for i in obstacles:
# left_block = abs()
def left(r_q, c_q, obstacles, now_q):
# not need to move but easy
# too many loop....
return c_q - now_q - 1
if (now_q > 1):
next_q = now_q - 1
return left(r_q, c_q, obstacles, next_q)
else:
return c_q - 1
# def right(..,)
def queensAttack(n, k, r_q, c_q, obstacles):
print(left(r_q, c_q, obstacles, c_q))
if __name__ == '__main__':
first_multiple_input = input().rstrip().split()
n = int(first_multiple_input0) k = int(first_multiple_input1) second_multiple_input = input().rstrip().split()
r_q = int(second_multiple_input0) c_q = int(second_multiple_input1) obstacles = []
for _ in range(k):
obstacles.append(list(map(int, input().rstrip().split())))
result = queensAttack(n, k, r_q, c_q, obstacles)
fptr.write(str(result) + '\n')
fptr.close()
解答
code: python
import math
import os
import random
import re
import sys
#
# Complete the 'queensAttack' function below.
#
# The function is expected to return an INTEGER.
# The function accepts following parameters:
# 1. INTEGER n
# 2. INTEGER k
# 3. INTEGER r_q
# 4. INTEGER c_q
# 5. 2D_INTEGER_ARRAY obstacles
#
def queensAttack(n, k, r_q, c_q, obstacles):
# Write your code here
# no obstacles
left = c_q - 1
right = n - c_q
up = n - r_q
down = r_q - 1
upleft = left if left < up else up
upright = right if right < up else up
downleft = left if left < down else down
downright = right if right < down else down
# obstacles are present
for obst in obstacles:
# exist at left
if row == r_q and col < c_q:
# more nearly
if c_q - col - 1 < left:
left = c_q - col - 1
# exist at right
elif row == r_q and col > c_q:
if col - c_q - 1 < right:
right = col - c_q - 1
# exist at up
elif row > r_q and col == c_q:
if row - r_q - 1 < up:
up = row - r_q - 1
# exist at down
elif row < r_q and col == c_q:
if r_q - row - 1 < down:
down = r_q - row - 1
# exist at upleft
# 象限
elif row > r_q and col < c_q:
# 直線上
if row - r_q == c_q - col:
if row - r_q - 1 < upleft:
upleft = row - r_q - 1
# exist at upright
elif row > r_q and col > c_q:
if row - r_q == col - c_q:
if row - r_q - 1 < upright:
upright = row - r_q - 1
# exist at downleft
elif row < r_q and col < c_q:
if r_q - row == c_q - col:
if r_q - row - 1 < downleft:
downleft = r_q - row - 1
# exist at downright
elif row < r_q and col > c_q:
if r_q - row == col - c_q:
if r_q - row - 1 < downright:
downright = r_q - row - 1
attack = left + right + up + down + upleft + upright + downleft + downright
return attack
if __name__ == '__main__':
first_multiple_input = input().rstrip().split()
n = int(first_multiple_input0) k = int(first_multiple_input1) second_multiple_input = input().rstrip().split()
r_q = int(second_multiple_input0) c_q = int(second_multiple_input1) obstacles = []
for _ in range(k):
obstacles.append(list(map(int, input().rstrip().split())))
result = queensAttack(n, k, r_q, c_q, obstacles)
fptr.write(str(result) + '\n')
fptr.close()
テーマ
メモ
https://www.youtube.com/watch?v=VQyo8Zy3s40
提出
RecursionError: maximum recursion depth exceeded in comparison
code: python
import math
import os
import random
import re
import sys
sys.setrecursionlimit(20000)
def search(visited, nowR, nowC, obstacles):
for i in range(8):
search(visited, nextC, nextR, obstacles)
#
# Complete the 'queensAttack' function below.
#
# The function is expected to return an INTEGER.
# The function accepts following parameters:
# 1. INTEGER n
# 2. INTEGER k
# 3. INTEGER r_q
# 4. INTEGER c_q
# 5. 2D_INTEGER_ARRAY obstacles
#
def queensAttack(n, k, r_q, c_q, obstacles):
# Write your code here
search(visited, r_q, c_q, obstacles)
print(visited)
if __name__ == '__main__':
first_multiple_input = input().rstrip().split()
n = int(first_multiple_input0) k = int(first_multiple_input1) second_multiple_input = input().rstrip().split()
r_q = int(second_multiple_input0) c_q = int(second_multiple_input1) obstacles = []
for _ in range(k):
obstacles.append(list(map(int, input().rstrip().split())))
result = queensAttack(n, k, r_q, c_q, obstacles)
fptr.write(str(result) + '\n')
fptr.close()